import { addPayer, updatePayer, deleteById, getById, getOutbuyPayerList, queryOutbuyPayerListById } from '../../services/outbuy/outbuyPayer';
import {message} from 'antd';
const R=require("ramda");

export default {
    namespace: 'outbuyPayer',
    state: {
        page: {},
        list: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }){
            return {...state, ...payload };
        }
    },
    effects: {
        /**
         * 获取境外贸易商收款人列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getOutbuyPayerList({payload}, { call, put, select }) {
            const res = yield call(getOutbuyPayerList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取单个详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getById({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(getById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                    if(payload.onSuccess) payload.onSuccess();
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },

        /**
         * 删除收款账户
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *deleteById({ payload }, { call, put, select }) {
            const res = yield call(deleteById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                message.info("删除收款账户成功");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 创建收款账户
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *addPayer({payload}, { call, put, select }) {
            const res = yield call(addPayer, payload);
            if (res.data.resultCode === "ok") {
                message.info("增加收款账户成功");
                if(payload.onSuccess)payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 修改收款账户
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *updatePayer({payload}, { call, put, select }) {
            const res = yield call(updatePayer, payload);
            if (res.data.resultCode === "ok") {
                message.info("修改收款账户成功");
                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 获取客户的商品供应商
         *
         * @param call
         * @param put
         * @param select
         */
            *queryOutbuyPayerListById({ payload }, { call, put, select }) {
            const res = yield call(queryOutbuyPayerListById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: R.isNil(content) ? [] : content
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },
    }
};
